package cn.wolfcode.service.impl;


import cn.wolfcode.domain.Permission;
import cn.wolfcode.mapper.PermissionMapper;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IPermissionService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Set;

@Service
@Transactional
public class PermissionServiceImpl implements IPermissionService {

    @Autowired
    private PermissionMapper permissionMapper;

    @Override
    public void delete(Long id) {
        permissionMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void insert(Permission permission) {
        permissionMapper.insert(permission);
    }

    @Override
    public Permission selectOne(Long id) {
        Permission permission = permissionMapper.selectByPrimaryKey(id);
        return permission;
    }

    @Override
    public List<Permission> selectAll() {
        List<Permission> permissions = permissionMapper.selectAll();
        return permissions;
    }

    @Override
    public void update(Permission permission) {
        permissionMapper.updateByPrimaryKey(permission);
    }

    @Override
    public PageInfo<Permission> query(QueryObject qo) {
        // 表明接下来的查询用分页

//        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());

        // 使用 bootstrap 框架后
        PageHelper.offsetPage(qo.getOffset(),qo.getLimit());//设置分页信息

        List<Permission> permissions = permissionMapper.selectForList(qo);
        return new PageInfo<>(permissions);
    }

    @Override
    public void savePermissions(Set<Permission> permissionList) {
        permissionMapper.savePermissions(permissionList);
    }

    @Override
    public List<Permission> selectPermissionById(Long id) {
        return permissionMapper.selectPermissionById(id);
    }

    @Override
    public List<String> selectPermissionsByEmpId(Long id) {
        return permissionMapper.selectPermissionsByEmpId(id);
    }

    @Override
    public List<String> selectPermissions() {

       return permissionMapper.selectPermissions();
    }

    @Override
    public Workbook export(){
        // 2 创建 workbook
        Workbook workBook = new XSSFWorkbook();
        //
        Sheet sheet = workBook.createSheet("权限信息列表");
        // 3 创建表头信息
        Row row = sheet.createRow(0);
        row.createCell(0).setCellValue("权限名称");
        row.createCell(1).setCellValue("权限表达式");
        // 4 查询数据库中所有的权限信息
        List<Permission> permissions = permissionMapper.selectAll();
        // 5 进行循环遍历
        for (int i = 0; i < permissions.size(); i++) {
            // 6 在遍历过程中创建 row --> 创建单元格  -->  给单元格设置值
            Permission permission = permissions.get(i);
            Row row1 = sheet.createRow(i + 1);
            row1.createCell(0).setCellValue(permission.getName());
            row1.createCell(1).setCellValue(permission.getExpression());
        }
        return workBook;
    }

    @Override
    public void importExcel(Workbook workbook){
        // 获取数据库中的权限表达式集合
        List<String> permissions = permissionMapper.selectPermissions();
        // 1 通过 Workbook 获取sheet
        Sheet sheet = workbook.getSheetAt(0);
        // 2 获取到最后一行
        int lastRowNum = sheet.getLastRowNum();
        // 3 循环遍历到最后一行; i从1开始，0是表头信息不需要
        for (int i = 1; i <= lastRowNum; i++) {
            //  3.1 拿到单元格 并且拿到单元格值
            Row row = sheet.getRow(i);
            String name = row.getCell(0).getStringCellValue();
            String expression = row.getCell(1).getStringCellValue();
            //  3.2 判断权限表达式是否在数据中存在,如果存在则抛异常
            if (permissions.contains(expression)){
                throw new RuntimeException("表达式"+ expression + "已存在");
            }
            //  3.3 如果不存在把信息封装到 Permission 中
            Permission permission = new Permission();
            permission.setName(name);
            permission.setExpression(expression);
            //  3.4 保存到数据库中
            permissionMapper.insert(permission);
        }
    }
}
